1 - Principles of Programming Languages [ID:11054]
50 von 713 angezeigt

So today is about functional programming. What is functional programming?

Functional programming is the idea that you should program your programs just like you would program math.

Programming math means evaluating functions, evaluating expressions.

You do not directly manipulate memory. You do not change values.

You only look at valid correct math.

X equals X plus one is not correct math because this is not an equality that you can ever solve.

This is always incorrect in math.

So what do you do in math?

In math you create an extra variable to hold the new value.

This is the idea of functional programming.

You're programming expressions that are mathematically correct.

That is the intention that we are going after.

So we're trying to be close to the math notation.

So Fx equals cos times sin.

So this is juxtaposition as well.

Juxtaposition means that you omit the multiply sign in the middle.

It's a little tricky for parsing.

The parser now has to know that cos is a function and that you're intending to call cos X and not cos brace X times sin X as an argument.

Is this the argument or is only that the argument?

That's a little tricky. This is juxtaposition as a problem of parsing.

What is intended instead of what is just written.

So all is a function. Function is all.

We are only looking at functions now.

This is the functional program.

This is not all.

Ada is not a functional programming language.

It has functions.

It even has function pointers.

That is not the all.

What we are meaning here is that functions should not have side effects.

The result of a function should only be determined by its arguments.

You call a function with a given set of arguments and that should be its return value and not some global variable that can externally influence what your function is doing.

Another idea of functional programming is that we leave out all these low level details that we have by imperative programming.

In imperative programming you have to allocate memory.

You get returned an address or some memory location.

You manipulate that memory and do something with it, perform your calculations and return something.

We want to abstract away from that here.

So it's a little bit towards higher level programming.

So the principle there is evaluating functions do not allow any side effects.

This makes it easier for both the compiler and the programmer to understand what your program is doing.

For example, currently in the research world, in the compiler world, it's a hot topic to think about parallelism.

How do I automatically parallelize my program given an imperative program?

This is hard, right?

Because in imperative programs, functions have side effects.

If you parallelize something, then these side effects can be interleaved in different ways.

That makes parallelism hard.

It makes it hard for the compiler to determine if this function can be run in parallel with that function.

However, you have a functional language.

Any two functions can be run in parallel without having to think about it.

So the compiler does not have to figure out if two functions can run in parallel.

Zugänglich über

Offener Zugang

Dauer

01:00:56 Min

Aufnahmedatum

2013-05-22

Hochgeladen am

2019-05-09 16:19:51

Sprache

en-US

Einbetten
Wordpress FAU Plugin
iFrame
Teilen